package com.one.shopping.util;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.one.shopping.R;

import java.util.Collection;
import java.util.Map;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/12 16:49
 */
public class CommonUtils {

    /**
     * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
     *
     * @param obj
     * @return
     */
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null)
            return true;

        if (obj instanceof CharSequence)
            return ((CharSequence) obj).length() == 0;

        if (obj instanceof Collection)
            return ((Collection) obj).isEmpty();

        if (obj instanceof Map)
            return ((Map) obj).isEmpty();

        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    public static void setImageForUrl(ImageView iv, String imgUrl) {

        iv.setVisibility(TextUtils.isEmpty(imgUrl)? View.GONE : View.VISIBLE);
        if (iv.getTag() == null || !iv.getTag().equals(imgUrl)) {
            iv.setImageResource(R.drawable.mini_avatar);
        }
        ImageLoader.getInstance().displayImage(imgUrl, iv, ImageLoaderUtils.getOption());
        iv.setTag(imgUrl);
    }
}
